package com.example.wordbook;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Dbmanager {
    private Dbhelper helper;

    public Dbmanager(Dbhelper db) {
        super();
        this.helper = db;
    }

    public boolean insertData(String tableName, ContentValues values) {
        SQLiteDatabase db = helper.getWritableDatabase();
        int i = (int) db.insert(tableName, null, values);
        db.close();
        if (i > 0) {
            return true;
        }
        return false;
    }

    // 查询数据
    @SuppressLint("Range")
    public ArrayList<String> selectData(String word) {
        String sql = "select * from word where egword='" + word + "'";
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor c = db.rawQuery(sql, null);
        ArrayList<String> str = new ArrayList<>();
        if(c.moveToFirst()) {
            String egword = c.getString(c.getColumnIndex("egword"));
            String cnword = c.getString(c.getColumnIndex("cnword"));
            String sample = c.getString(c.getColumnIndex("sample"));
                str.add(egword); str.add(cnword);
            str.add(sample);
        }
        else {
            return null;
        }
        db.close();
        return str;
    }

    @SuppressLint("Range")
    public ArrayList<String> quey(String word) {
        SQLiteDatabase db = helper.getReadableDatabase();
        String sql = "select * from word where egword like '" + word + "%'";
        Cursor c = db.rawQuery(sql, null);
        ArrayList<String> resultlist = new ArrayList<String>();
        while (c.moveToNext()) {
            String egword = c.getString(c.getColumnIndex("egword"));
            egword.contains(word);
            if(!resultlist.contains(egword)){
            resultlist.add(egword);
            }

        }

        return resultlist;
}
    public boolean changeData(String word, String chinese){
        String sql="update [word] set cnword='"+chinese+"' where egword='"+word+"'";
        SQLiteDatabase db=helper.getWritableDatabase();
        db.execSQL(sql);
        Cursor c=db.rawQuery("select * from word where egword=?",new String[]{word});
        boolean i=false;
        if(c.moveToFirst()) {
            @SuppressLint("Range") String ch = c.getString(c.getColumnIndex("cnword"));
            if (!ch.equals(chinese)){
                i=true;
            }
        }
        db.close();
        return i;
    }
    @SuppressLint("Range")
    public boolean deleteData(String word){
        String sql="delete from word where egword='"+word+"'";
        SQLiteDatabase db=helper.getWritableDatabase();
        db.execSQL(sql);
        Cursor c=db.rawQuery("select * from word where egword=?",new String[]{word});
        boolean i=false;
        if (!c.moveToFirst()){
            i=true;
        }
        db.close();
        return i;
    }
    public HashMap<String,String> getwords(){
        HashMap<String,String> words=new HashMap<>();
        SQLiteDatabase db=helper.getReadableDatabase();
        Cursor c=db.rawQuery("select * from word ",null);
        while(c.moveToNext()){
            @SuppressLint("Range") String egword = c.getString(c.getColumnIndex("egword"));
            @SuppressLint("Range") String cnword=c.getString(c.getColumnIndex("cnword"));
            words.put(egword,cnword);
        }
        return words;
    }
    public ArrayList<String>getegwords(){
        ArrayList<String>words=new ArrayList<>();
        SQLiteDatabase db=helper.getReadableDatabase();
        Cursor c=db.rawQuery("select * from word ",null);
        while(c.moveToNext()){
            @SuppressLint("Range") String egword = c.getString(c.getColumnIndex("egword"));
            words.add(egword);
        }
        return words;
    }
    public boolean insertsample(String sample,String egword){
        boolean i=false;
        SQLiteDatabase db=helper.getWritableDatabase();
        String sql="update [word] set sample='"+sample+"' where egword='"+egword+"'";
        db.execSQL(sql);
        Cursor c=db.rawQuery("select * from word where egword='"+egword+"'",null);
        if(c.moveToFirst()){
            @SuppressLint("Range") String sam=c.getString(c.getColumnIndex("sample")).trim();
            if(sam.equals(sample)){
                i=true;
            }
        }
        return i;
    }
}
